package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Controllor.DB;
import Model.Apartment;
import Model.Lease;
import Model.Employee;
import Model.Person;
import Model.Resident;

import javax.swing.JLabel;

import java.awt.FlowLayout;

import javax.swing.JButton;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;

public class LeaseForm extends JFrame {

	private JPanel contentPane;
	private JComboBox<String> comboBox_2;
	private JComboBox<String> comboBox_1;
	private JComboBox<String> comboBox;
	private Lease lease;
	private JTextField startDateFieldBox;
	private JTextField termFieldBox;
	private JTextField depositFieldBox;
	private JTextField discountFieldBox;
	private JTextField petsFieldBox;
	private JTextField petDepositFieldBox;
	private JTextField insuranceCarrierFieldBox;
	private JTextField insuranceIDFieldText;
	
	private List<Apartment> apartments;
	private List<Employee> employees;
	private List<Resident> residents;
	private DB database;
	private boolean isEditing;
	
	public LeaseForm(DB database) {
		this(null, database, false);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public LeaseForm(Lease lease, final DB database, final boolean isEditing) {
		this.isEditing = isEditing;
		this.database = database;
		//Go away
		employees = database.getEmployees();
		residents = database.getResidents();
		if (lease != null)
			this.lease = lease;
		apartments = database.getApartments();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 490, 379);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Submit");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String startDate = startDateFieldBox.getText();
				int term = Integer.parseInt(termFieldBox.getText());
				double deposit = Double.parseDouble(depositFieldBox.getText());
				double monthlyDiscount = Double.parseDouble(discountFieldBox.getText());
				int pets = Integer.parseInt(petsFieldBox.getText());
				double petDeposit = Double.parseDouble(petDepositFieldBox.getText());
				String insuranceCarrier = insuranceCarrierFieldBox.getText();
				String insuranceID = insuranceIDFieldText.getText();
				String apartmentNumber;
				if (comboBox_2.getItemCount() == 1) {
					apartmentNumber = comboBox_2.getItemAt(0).toString().substring(0, 4);
				} else {
					apartmentNumber = comboBox_2.getSelectedItem().toString().substring(0, 4);	
				}
				int residentID;
				if (comboBox_1.getItemCount() == 1) {
					residentID = Integer.parseInt(comboBox_1.getItemAt(0).substring(0, 1));
				} else if (comboBox_1.getItemCount() > 1){
					residentID = Integer.parseInt(comboBox_1.getSelectedItem().toString().substring(0, 1));
				} else {
					residentID = 1;
				}
				int employeeID;
				if (comboBox.getItemCount() == 1) {
					employeeID = Integer.parseInt(comboBox.getItemAt(0).substring(0, 1));
				} else {
					employeeID = Integer.parseInt(comboBox.getSelectedItem().toString().substring(0, 1));	
				}
				if (isEditing) {
					database.updateLease(startDate, term, deposit, monthlyDiscount, pets, petDeposit, insuranceCarrier, insuranceID, apartmentNumber, employeeID, residentID);
				} else {
					database.addLease(startDate, term, deposit, monthlyDiscount, pets, petDeposit, insuranceCarrier, insuranceID, apartmentNumber, employeeID, residentID);	
				}
				
				dispose();
			}
		});
		panel.add(btnNewButton_1);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel lblStartDate = new JLabel("Start Date");
		lblStartDate.setBounds(32, 116, 77, 14);
		panel_1.add(lblStartDate);
		
		JLabel lblTerm = new JLabel("Term");
		lblTerm.setBounds(58, 142, 51, 14);
		panel_1.add(lblTerm);
		
		JLabel lblDeposit = new JLabel("Deposit");
		lblDeposit.setBounds(275, 187, 50, 14);
		panel_1.add(lblDeposit);
		
		JLabel lblDiscount = new JLabel("Discount");
		lblDiscount.setBounds(269, 212, 56, 14);
		panel_1.add(lblDiscount);
		
		JLabel lblPets = new JLabel("Pets");
		lblPets.setBounds(61, 187, 48, 14);
		panel_1.add(lblPets);
		
		JLabel lblPetDeposit = new JLabel("Pet Deposit");
		lblPetDeposit.setBounds(29, 212, 68, 14);
		panel_1.add(lblPetDeposit);
		
		startDateFieldBox = new JTextField();
		startDateFieldBox.setBounds(128, 113, 86, 20);
		startDateFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			startDateFieldBox.setText(lease.getStartDate());
		} else {
			startDateFieldBox.setText("--/--/----");	
		}
		
		panel_1.add(startDateFieldBox);
		startDateFieldBox.setColumns(10);
		
		termFieldBox = new JTextField();
		termFieldBox.setBounds(128, 139, 86, 20);
		termFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			termFieldBox.setText(lease.getTerm() + "");
		}
		panel_1.add(termFieldBox);
		termFieldBox.setColumns(10);
		
		comboBox = new JComboBox();
		comboBox.setBounds(129, 38, 179, 20);
		if (lease != null) {
			List<Employee> employees = database.getEmployees();
			Person p = null;
			for (int i = 0; i < employees.size(); i++) {
				if (employees.get(i).getID() == lease.getEmployeeID()) {
					p = database.getPerson(employees.get(i).getPersonID());
					break;					
				}
			}
			comboBox.addItem(lease.getEmployeeID() + "| " + p.getFName() + " " + p.getMInit() + " " + p.getLName());
			comboBox.setEnabled(false);
		} else {
			comboBox.insertItemAt("", 0);
			Person p;
			for (int i = 1; i < employees.size() + 1; i++) {
				p = database.getPerson(residents.get(i - 1).getPersonID());
				comboBox.addItem(employees.get(i - 1).getID() + "| " + p.getFName() + " " + p.getMInit() + " " + p.getLName());
			}
		}
			
		panel_1.add(comboBox);
		
		JLabel lblEmployeeId = new JLabel("Employee ID");
		lblEmployeeId.setBounds(22, 41, 97, 14);
		panel_1.add(lblEmployeeId);
		
		comboBox_1 = new JComboBox();
		comboBox_1.setBounds(129, 10, 179, 20);
		if (lease != null) {
			Person p = database.getPerson(lease.getResidentID());
			comboBox_1.addItem(lease.getResidentID() + "| " + p.getFName() + " " + p.getMInit() + " " + p.getLName());
			comboBox_1.setEnabled(false);
		} else {
			comboBox_1.insertItemAt("", 0);
			residents = database.getResidents();
			Person p;
			for (int i = 1; i < residents.size() + 1; i++) {				
				p = database.getPerson(residents.get(i - 1).getPersonID());
				comboBox_1.addItem(residents.get(i - 1).getID() + "| " + p.getFName() + " " + p.getMInit() + " " + p.getLName());
			}
		}
		panel_1.add(comboBox_1);
		
		JLabel lblResident = new JLabel("Resident");
		lblResident.setBounds(42, 13, 77, 14);
		panel_1.add(lblResident);
		
		JButton btnNewButton_2 = new JButton("Add Resident");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ResidentForm(database);
				dispose();
			}
		});
		if (lease != null)
			btnNewButton_2.setVisible(false);
		btnNewButton_2.setBounds(317, 9, 118, 23);
		panel_1.add(btnNewButton_2);
		
		depositFieldBox = new JTextField();
		depositFieldBox.setBounds(328, 184, 107, 20);
		depositFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			depositFieldBox.setText(lease.getDepositPaid() + "");
		}
		panel_1.add(depositFieldBox);
		depositFieldBox.setColumns(10);
		
		discountFieldBox = new JTextField();
		discountFieldBox.setBounds(328, 209, 107, 20);
		discountFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			discountFieldBox.setText(lease.getMonthlyDiscount() + "");
		}
		panel_1.add(discountFieldBox);
		discountFieldBox.setColumns(10);
		
		petsFieldBox = new JTextField();
		petsFieldBox.setBounds(128, 184, 86, 20);
		petsFieldBox.setColumns(10);
		petsFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			petsFieldBox.setText(lease.getNumberOfPets() + "");
		}
		panel_1.add(petsFieldBox);
		
		petDepositFieldBox = new JTextField();
		petDepositFieldBox.setBounds(128, 209, 86, 20);
		petDepositFieldBox.setColumns(10);
		petDepositFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			petDepositFieldBox.setText(lease.getPetDepositPaid() + "");
		}
		panel_1.add(petDepositFieldBox);
		
		insuranceCarrierFieldBox = new JTextField();
		insuranceCarrierFieldBox.setBounds(163, 258, 272, 20);
		insuranceCarrierFieldBox.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			insuranceCarrierFieldBox.setText(lease.getRentersInsuranceCarrier() + "");
		}
		panel_1.add(insuranceCarrierFieldBox);
		insuranceCarrierFieldBox.setColumns(10);
		
		JLabel lblInsuranceCarrier = new JLabel("Insurance Carrier");
		lblInsuranceCarrier.setBounds(44, 261, 109, 14);
		panel_1.add(lblInsuranceCarrier);
		
		JLabel lblInsuranceId = new JLabel("Insurance ID");
		lblInsuranceId.setBounds(42, 284, 99, 14);
		panel_1.add(lblInsuranceId);
		
		insuranceIDFieldText = new JTextField();
		insuranceIDFieldText.setBounds(163, 281, 272, 20);
		insuranceIDFieldText.setColumns(10);
		insuranceIDFieldText.setHorizontalAlignment(SwingConstants.CENTER);
		if (lease != null) {
			insuranceIDFieldText.setText(lease.getRentersInsuranceID() + "");
		}
		panel_1.add(insuranceIDFieldText);
		
		comboBox_2 = new JComboBox<String>();
		comboBox_2.setBounds(128, 82, 307, 20);
		if (lease != null) {
			List<Apartment> apartments = database.getApartments();
			Apartment a = null;
			for (int i = 0; i < apartments.size(); i++) {
				if (apartments.get(i).getNumber().equals(lease.getApartmentNumber())) {
					a = apartments.get(i);
					break;
				}
			}
			comboBox_2.addItem(lease.getApartmentNumber() + "    Plane: " + a.getfloorPlan() + "   Sq ft: " + a.getSize() + "    Cost: " + a.getbaseCost());
			comboBox_2.setEnabled(false);
		} else {
			comboBox_2.insertItemAt("", 0);
			for (int i = 1; i < apartments.size() + 1; i++) {
				comboBox_2.addItem(apartments.get(i - 1).getNumber() + "    Plane: " + apartments.get(i - 1).getfloorPlan() + "   Sq ft: " + apartments.get(i - 1).getSize() + "    Cost: " + apartments.get(i - 1).getbaseCost());
			}
		}
		panel_1.add(comboBox_2);
		
		JLabel lblNewLabel = new JLabel("Apartment");
		lblNewLabel.setBounds(31, 82, 88, 14);
		panel_1.add(lblNewLabel);
		this.setVisible(true);
		
		
		
	}
}
